import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class SimpleApiTest {
    private static final String BASE_URL = "http://localhost:8080";
    
    public static void main(String[] args) {
        System.out.println("开始API测试...");
        
        // 测试服务可用性
        testServiceAvailability();
        
        // 测试健康检查接口
        testHealthCheck();
        
        System.out.println("API测试完成");
    }
    
    private static void testServiceAvailability() {
        System.out.println("\n========== 服务可用性测试 ==========");
        try {
            String url = BASE_URL + "/";
            String response = sendGetRequest(url);
            System.out.println("响应状态: 成功");
            System.out.println("响应体: " + (response.length() > 200 ? response.substring(0, 200) + "..." : response));
            System.out.println("========== 服务可用性测试结束 ==========\n");
        } catch (Exception e) {
            System.err.println("服务不可用: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static void testHealthCheck() {
        System.out.println("\n========== 健康检查测试 ==========");
        try {
            String url = BASE_URL + "/api/health";
            String response = sendGetRequest(url);
            System.out.println("响应状态: 成功");
            System.out.println("响应体: " + response);
            System.out.println("========== 健康检查测试结束 ==========\n");
        } catch (Exception e) {
            System.err.println("健康检查失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static String sendGetRequest(String urlString) throws IOException {
        System.out.println("请求URL: " + urlString);
        System.out.println("请求方法: GET");
        
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        
        // 设置请求头
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("User-Agent", "SimpleApiTest/1.0");
        System.out.println("请求头:");
        System.out.println("  Accept: " + connection.getRequestProperty("Accept"));
        System.out.println("  User-Agent: " + connection.getRequestProperty("User-Agent"));
        
        int responseCode = connection.getResponseCode();
        System.out.println("响应状态码: " + responseCode);
        
        // 打印响应头
        System.out.println("响应头:");
        for (String key : connection.getHeaderFields().keySet()) {
            if (key != null) {
                System.out.println("  " + key + ": " + connection.getHeaderField(key));
            }
        }
        
        // 读取响应体
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        
        System.out.println("响应体: " + response.toString());
        
        return response.toString();
    }
}